﻿using Aribaccio;
using BDPAutodetect.DataService;
using BDPAutodetect.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// 从机设备管理
    /// </summary>
    public partial class SlaveManager : BaseManager
    {
        /// <summary>
        /// 基础数据操作
        /// </summary>
        protected IDataServiceBasic DataService { get; set; }
        /// <summary>
        /// 从机设备集
        /// </summary>
        public List<MSlave> Slaves { get; protected set; }
        /// <summary>
        /// 是否有效存在工位信息
        /// </summary>
        public bool IsValid => this.Slaves != null && this.Slaves.Count > 1;

        /// <summary>
        /// 构造函数[注入]
        /// </summary>
        public SlaveManager(IDataServiceBasic argBusiness)
            : base("从机设备管理中心")
        {
            this.DataService = argBusiness;
            this.Slaves = new List<MSlave>() { new MSlave() };
        }

        /// <summary>
        /// 初始化工位数据
        /// </summary>
        public override async Task Initialize()
        {
            if (this.IsValid) return;
            if (this.Slaves == null) this.Slaves = new List<MSlave>();
            //获取数据库数据
            var resultDatas = this.DataService.SelectModelItemsTask<TBasicSlave>();
            await resultDatas.ConfigureAwait(false);
            if (resultDatas == null || resultDatas.Result == null || !resultDatas.Result.ResultState || resultDatas.Result.Data == null) return;

            foreach (var slaveItem in resultDatas.Result.Data)
            {
                if (slaveItem == null) continue;
                this.Slaves.Add(new MSlave(slaveItem.ID, slaveItem.Name, slaveItem.NameNick));
            }
        }

        /// <summary>
        /// 新增从机设备数据
        /// </summary>
        /// <returns></returns>
        public async Task<MSlave?> InsertSlave(MSlave argSlave)
        {
            TBasicSlave slave = argSlave.CloneTData();
            if (this.CheckExsits(slave)) return null;

            var result = this.DataService.InsertTask(slave);
            await result.ConfigureAwait(false);
            if (result == null || result.Result == null || !result.Result.ResultState) return null;

            var resultData = this.DataService.SelectModelItemTask<TBasicSlave>(v => v.Name == slave.Name);
            await resultData.ConfigureAwait(false);
            if (resultData == null || resultData.Result == null || !resultData.Result.ResultState || resultData.Result.Data == null) return null;

            MSlave slaveData = new MSlave(resultData.Result.Data.ID, resultData.Result.Data.Name, resultData.Result.Data.NameNick);
            this.Slaves.Add(slaveData);
            return slaveData;
        }

        /// <summary>
        /// 删除从机设备数据
        /// </summary>
        /// <returns></returns>
        public async Task<bool> DeleteSlave(MSlave argSlave)
        {
            if (argSlave == null) return false;
            Task<Result> result = this.DataService.DeleteWhereTask<TBasicSlave>(v => v.ID == argSlave.ID);
            await result.ConfigureAwait(false);
            if (result != null && result.Result.ResultState)
            {
                //移除缓存
                var currSlave = this.Slaves.First(v => v.ID == argSlave.ID);
                this.Slaves.Remove(currSlave);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 更新从机设备数据
        /// </summary>
        /// <param name="argSlave"></param>
        /// <returns></returns>
        public async Task<bool> UpdateSlave(MSlave argSlave)
        {
            if (argSlave == null) return false;
            TBasicSlave slave = argSlave.CloneTData();
            if (this.CheckExsits(slave)) return false;

            var result = this.DataService.UpdateWhereTask<TBasicSlave>(slave, v => v.ID == slave.ID);
            await result.ConfigureAwait(false);
            if (result != null && result.Result != null && result.Result.ResultState)
            {
                var slaveData = this.Slaves.First(v => v.ID == argSlave.ID);
                slaveData.Name = argSlave.Name;
                slaveData.NameNick = argSlave.NameNick;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 检测从机设备名称是否存在
        /// </summary>
        /// <param name="argSlave"></param>
        /// <returns></returns>
        public bool CheckExsits(TBasicSlave argSlave)
        {
            Result check = argSlave.ID > 0
                ? this.DataService.Exsits<TBasicSlave>(v => v.ID != argSlave.ID && v.Name.Equals(argSlave.Name, StringComparison.OrdinalIgnoreCase))
                : this.DataService.Exsits<TBasicSlave>(v => v.Name.Equals(argSlave.Name, StringComparison.OrdinalIgnoreCase));
            return (check != null && check.ResultState);
        }

        /// <summary>
        /// 获取从机设备信息
        /// </summary>
        /// <param name="argSlaveID">从机设备ID</param>
        /// <returns></returns>
        public MSlave? GetSlave(int argSlaveID)
        {
            if (!this.IsValid) return default;
            return this.Slaves.FirstOrDefault(v => v.ID == argSlaveID);
        }
    }
}
